﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Productions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Productions
{
    public class ProWorkorderServices:IProWorkorder
    {
        public IBaseRepository Db { get; }

        public ProWorkorderServices(IBaseRepository db)
        {
            Db = db;
        }

        public async Task<IEnumerable<ProWorkorder>> GatAllAsync()
        {
            string sql = "select * from ProWorkorder";
            var list = await Db.QueryAllAsync<ProWorkorder>(sql);
            return list;
        }

        public async Task<IEnumerable<ProWorkorder>> GetAsync(int PageIndex, int PageSize, string WorkorderCode, string WorkorderName, string ProductCode, string ProductName, string ClientName, string RequestDate)
        {
            string sql = "select * from ProWorkorder where 1=1 ";

            if (!string.IsNullOrEmpty(WorkorderCode))
            {
                sql += " and WorkorderCode like @WorkorderCode ";
            }
            if (!string.IsNullOrEmpty(WorkorderName))
            {
                sql += " and WorkorderName like @WorkorderName ";
            }
            if (!string.IsNullOrEmpty(ProductCode))
            {
                sql += " and ProductCode like @ProductCode ";
            }
            if (!string.IsNullOrEmpty(ProductName))
            {
                sql += " and ProductName like @ProductName ";
            }
            if (!string.IsNullOrEmpty(ClientName))
            {
                sql += " and ClientName like @ClientName ";
            }
            if (!string.IsNullOrEmpty(RequestDate))
            {
                sql += " and RequestDate like @RequestDate ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await Db.QueryAsync<ProWorkorder>(sql, new { PageIndex, PageSize, WorkorderCode = $"%{WorkorderCode}%", WorkorderName = $"%{WorkorderName}%", ProductCode = $"%{ProductCode}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string WorkorderCode, string WorkorderName, string ProductCode, string ProductName, string ClientName, string RequestDate)
        {
            string sql = "select count(*) from ProWorkorder where 1=1 ";


            if (!string.IsNullOrEmpty(WorkorderCode))
            {
                sql += " and WorkorderCode like @WorkorderCode ";
            }

            if (!string.IsNullOrEmpty(WorkorderName))
            {
                sql += " and WorkorderName like @WorkorderName ";
            }

            if (!string.IsNullOrEmpty(ProductCode))
            {
                sql += " and ProductCode like @ProductCode ";
            }
            if (!string.IsNullOrEmpty(ProductName))
            {
                sql += " and ProductName like @ProductName ";
            }
            if (!string.IsNullOrEmpty(ClientName))
            {
                sql += " and ClientName like @ClientName ";
            }
            if (!string.IsNullOrEmpty(RequestDate))
            {
                sql += " and RequestDate like @RequestDate ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { WorkorderCode = $"%{WorkorderCode}%", WorkorderName = $"%{WorkorderName}%", ProductCode = $"%{ProductCode}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(ProWorkorder model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }

        public async Task<bool> AddAsync(ProWorkorder model)
        {

            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {

            string sql = "delete ProWorkorder where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete ProWorkorder where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }




        public async Task<IEnumerable<ProWorkorder>> GetNonPickAsync(int PageIndex, int PageSize, string WorkorderCode, string WorkorderName, string ProductCode, string ProductName, string ClientName, string RequestDate)
        {
            string sql = "select * from ProWorkorder where WorkorderCode not in(select WorkorderCode from OrderProductPick) ";

            if (!string.IsNullOrEmpty(WorkorderCode))
            {
                sql += " and WorkorderCode like @WorkorderCode ";
            }
            if (!string.IsNullOrEmpty(WorkorderName))
            {
                sql += " and WorkorderName like @WorkorderName ";
            }
            if (!string.IsNullOrEmpty(ProductCode))
            {
                sql += " and ProductCode like @ProductCode ";
            }
            if (!string.IsNullOrEmpty(ProductName))
            {
                sql += " and ProductName like @ProductName ";
            }
            if (!string.IsNullOrEmpty(ClientName))
            {
                sql += " and ClientName like @ClientName ";
            }
            if (!string.IsNullOrEmpty(RequestDate))
            {
                sql += " and RequestDate like @RequestDate ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await Db.QueryAsync<ProWorkorder>(sql, new { PageIndex, PageSize, WorkorderCode = $"%{WorkorderCode}%", WorkorderName = $"%{WorkorderName}%", ProductCode = $"%{ProductCode}%" });
            return list;
        }

        public async Task<int> GetNonPickCountAsync(string WorkorderCode, string WorkorderName, string ProductCode, string ProductName, string ClientName, string RequestDate)
        {
            string sql = "select count(*) from ProWorkorder where WorkorderCode not in(select WorkorderCode from OrderProductPick)  ";


            if (!string.IsNullOrEmpty(WorkorderCode))
            {
                sql += " and WorkorderCode like @WorkorderCode ";
            }

            if (!string.IsNullOrEmpty(WorkorderName))
            {
                sql += " and WorkorderName like @WorkorderName ";
            }

            if (!string.IsNullOrEmpty(ProductCode))
            {
                sql += " and ProductCode like @ProductCode ";
            }
            if (!string.IsNullOrEmpty(ProductName))
            {
                sql += " and ProductName like @ProductName ";
            }
            if (!string.IsNullOrEmpty(ClientName))
            {
                sql += " and ClientName like @ClientName ";
            }
            if (!string.IsNullOrEmpty(RequestDate))
            {
                sql += " and RequestDate like @RequestDate ";
            }
            var count = await Db.GetRecoreCountAsync(sql, new { WorkorderCode = $"%{WorkorderCode}%", WorkorderName = $"%{WorkorderName}%", ProductCode = $"%{ProductCode}%" });
            return count;
        }

    }
}
